package com.zeng.controller;



import com.zeng.service.ComplaintsService;
import com.zeng.service.UserinfoService;
import com.zeng.entity.Complaints;
import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;
import com.zeng.entity.Userinfo;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Complaints)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-30 14:49:42
 */
@RestController
@RequestMapping("complaints")
public class ComplaintsController {
    /**
     * 服务对象
     */
    @Resource
    private ComplaintsService complaintsService;
    @Resource
    private UserinfoService userinfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Complaints selectOne(Integer id) {
        return this.complaintsService.queryById(id);
    }

    @GetMapping("fenghao")
    public Result fenghao(Integer id) {
        Complaints complaints = this.complaintsService.queryById(id);
        complaints.setState("已处理");
        this.complaintsService.update(complaints);
        //修改用户表
        Userinfo userinfo = this.userinfoService.queryById(complaints.getEduid());
        userinfo.setUserstate(1);
        this.userinfoService.update(userinfo);

        return Result.success("成功封禁");
    }

    @GetMapping("bohuitousu")
    public Result bohuitousu(Integer id) {
        Complaints complaints = this.complaintsService.queryById(id);
        complaints.setState("已处理");
        this.complaintsService.update(complaints);
        return Result.success("成功驳回");
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<Complaints> selectAll() {
        return this.complaintsService.selectAll();
    }

    /**
     * 根据页号查询
     *
     * @param offset 查询初始位置
     * @param limit  查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public ResultPage selectAllByLimit(int offset, int limit) {
        int pageTotal = this.complaintsService.queryCount();
        List<Complaints> list = this.complaintsService.queryAllByLimit(offset, limit);
        return ResultPage.success(pageTotal, "查询成功", list);
    }

    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        if (map.get("key").equals("")) {
            int pageTotal = this.complaintsService.queryCount();
            List<Complaints> list = this.complaintsService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        } else {
            int pageTotal = this.complaintsService.queryCountSelect(map.get("key"));
            List<Complaints> list = this.complaintsService.queryKeyByLimit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        }
    }

    /**
     * 根据主键删除
     *
     * @return
     */
    @GetMapping("deleteById")
    public Result deleteById(Integer id) {
        this.complaintsService.deleteById(id);
        return Result.success("删除成功");
    }

//    @GetMapping("deleteByFlag")
//    public Result deleteByFlag(Integer id) {
//        Complaints complaints = complaintsService.queryById(id);
//        complaints.setDeleteflag(1);
//        complaintsService.update(complaints);
//        return Result.success("删除成功");
//    }

    @RequestMapping("edit")
    public Result edit(@RequestBody HashMap<String, String> map
    ) {
        Complaints complaints = this.complaintsService.queryById(Integer.valueOf(map.get("complaintsid")));
        complaints.setDetails(map.get("details"));
        complaints.setUid(Integer.valueOf(map.get("uid")));
        complaints.setEduid(Integer.valueOf(map.get("eduid")));
        complaints.setState(map.get("state"));
        complaints.setCreatetime(TimeUtils.NowTimeN());
        complaintsService.update(complaints);
        return Result.success("修改成功");
    }

    @RequestMapping("add")
    public Result add(@RequestBody HashMap<String, String> map
    ) {
        Complaints complaints = new Complaints();
        complaints.setDetails(map.get("details"));
        complaints.setUid(Integer.valueOf(map.get("uid")));
        complaints.setEduid(Integer.valueOf(map.get("eduid")));
        complaints.setState(map.get("state"));
        complaints.setCreatetime(TimeUtils.NowTimeN());
        complaintsService.insert(complaints);
        return Result.success("添加成功");
    }
}
